﻿using Ec.NETCoreFramework.Common.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Ec.NETCoreFramework.Common.Core
{
    public static class DictionaryExtend
    {
        public static Dictionary<string, T> ToOrdinalIgnoreCase<T>(this Dictionary<string, T> dic)
        {
            return new Dictionary<string, T>(dic, StringComparer.OrdinalIgnoreCase);
        }
        /// <summary>
        /// 对数据字典中的值进行排序和过滤.
        /// </summary>
        /// <param name="dicArray">需要处理的字典.</param>
        /// <returns></returns>
        public static Dictionary<string, object> SortFilter(this Dictionary<string, object> dicArray)
        {
            return dicArray.SortFilter(true, null);
        }

        /// <summary>
        /// 对数据字典中的值进行排序和过滤.
        /// </summary>
        /// <param name="dicArray">需要处理的字典.</param>
        /// <param name="filterKeys">需要过滤的关键字.</param>
        /// <returns></returns>
        public static Dictionary<string, object> SortFilter(this Dictionary<string, object> dicArray, string[] filterKeys)
        {
            return dicArray.SortFilter(true, filterKeys);
        }

        /// <summary>
        /// 对数据字典中的值进行排序和过滤.
        /// </summary>
        /// <param name="dicArray">需要处理的字典.</param>
        /// <param name="sort">是否进行从a-z的排序.</param>
        /// <param name="filterKeys">需要过滤的关键字.</param>
        /// <returns></returns>
        public static Dictionary<string, object> SortFilter(this Dictionary<string, object> dicArray, bool sort, string[] filterKeys)
        {
            Dictionary<string, object> retArray = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            try
            {
                foreach (KeyValuePair<string, object> temp in dicArray)
                {
                    if (temp.Value != "" && temp.Value != null)
                    {
                        if (filterKeys == null)
                        {
                            retArray.Add(temp.Key, temp.Value);
                        }
                        else
                        {
                            if (!filterKeys.Contains<string>(temp.Key))
                            {
                                retArray.Add(temp.Key, temp.Value);
                            }
                        }
                    }
                }

                if (sort)
                {
                    SortedDictionary<string, object> dicTemp = new SortedDictionary<string, object>(retArray);
                    retArray = new Dictionary<string, object>(dicTemp);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Instance.Error("Cache-Redis" + ex.Message);
            }

            return retArray;

        }
        /// <summary>
        /// 把数组所有元素，按照“参数=参数值”的模式用“ ＆ ”字符拼接成字符串，并对参数值做urlencode
        /// </summary>
        /// <param name="dicArray">需要拼接的数组</param>
        /// <returns>拼接完成以后的字符串</returns>
        public static string ToLinkString(this Dictionary<string, object> dicArray)
        {
            return dicArray.ToLinkString(null);
        }
     
        /// <summary>
        /// 把数组所有元素，按照“参数=参数值”的模式用“ ＆ ”字符拼接成字符串，并对参数值做urlencode
        /// </summary>
        /// <param name="dicArray"></param>
        /// <param name="encoder"></param>
        /// <returns></returns>
        public static string ToLinkString(this Dictionary<string, object> dicArray, Encoding encoder)
        {
            return dicArray.ToLinkString(encoder, false);
        }
        /// <summary>
        /// 把数组所有元素，按照“参数=参数值”的模式用“ ＆ ”字符拼接成字符串，并对参数值做urlencode
        /// </summary>
        /// <param name="dicArray">需要拼接的数组</param>
        /// <param name="encoder">字符编码</param>
        /// <param name="filterEmpty"></param>
        /// <returns>拼接完成以后的字符串</returns>
        public static string ToLinkString(this Dictionary<string, object> dicArray, Encoding encoder, bool filterEmpty)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                foreach (KeyValuePair<string, object> temp in dicArray)
                {
                    object val = temp.Value;
                    if ((!filterEmpty && val != null) || (filterEmpty && !string.IsNullOrWhiteSpace(System.Convert.ToString(val))))
                    {
                        if (encoder != null)
                        {

                            sb.Append(temp.Key + "=" + HttpUtility.UrlEncode(val.ToString(), encoder) + "&");
                        }
                        else
                        {
                            sb.Append(temp.Key + "=" + temp.Value + "&");
                        }
                    }
                }

                if (sb.ToString().EndsWith("&"))
                {
                    sb.Remove(sb.Length - 1, 1);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Instance.Error("Cache-Redis" + ex.Message);
            }

            return sb.ToString();
        }
        public static string ToLinkString(this IDictionary<string, object> dicArray)
        {
            return dicArray.ToLinkString(null, false);
        }
        public static string ToLinkString(this IDictionary<string, object> dicArray, Encoding encoder)
        {
            return dicArray.ToLinkString(encoder, false);
        }
        public static string ToLinkString(this IDictionary<string, object> dicArray, Encoding encoder, bool filterEmpty)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                foreach (KeyValuePair<string, object> temp in dicArray)
                {
                    object val = temp.Value;
                    if ((!filterEmpty && val != null) || (filterEmpty && !string.IsNullOrWhiteSpace(System.Convert.ToString(val))))
                    {
                        if (encoder != null)
                        {

                            sb.Append(temp.Key + "=" + HttpUtility.UrlEncode(val.ToString(), encoder) + "&");
                        }
                        else
                        {
                            sb.Append(temp.Key + "=" + temp.Value + "&");
                        }
                    }
                }

                if (sb.ToString().EndsWith("&"))
                {
                    sb.Remove(sb.Length - 1, 1);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Instance.Error("Cache-Redis" + ex.Message);
            }

            return sb.ToString();
        }
        /// <summary>
        /// 将字符串型字典转换为url型字符
        /// </summary>
        /// <param name="dic">待处理字典.</param>
        /// <param name="encoding">编码方式.</param>
        /// <param name="filterEmpty">是否过滤空值.</param>
        /// <returns>String</returns>
        /// <remarks>
        ///   <list>
        ///    <item><description>添加对字符串字典转换 added by xiepeng 2018/9/20</description></item>
        ///   </list>
        /// </remarks>
        public static string ToLinkString(this Dictionary<string, string> dic, bool filterEmpty = false, string encoding = "")
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                foreach (KeyValuePair<string, string> temp in dic)
                {
                    object val = temp.Value;
                    if ((!filterEmpty && val != null) || (filterEmpty && !string.IsNullOrWhiteSpace(System.Convert.ToString(val))))
                    {
                        if (!string.IsNullOrEmpty(encoding))
                        {
                            Encoding encoder = Encoding.GetEncoding(encoding);
                            sb.Append(temp.Key + "=" + HttpUtility.UrlEncode(val.ToString(), encoder) + "&");
                        }
                        else
                        {
                            sb.Append(temp.Key + "=" + temp.Value + "&");
                        }
                    }
                }

                if (sb.ToString().EndsWith("&"))
                {
                    sb.Remove(sb.Length - 1, 1);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Instance.Error("Cache-Redis" + ex.Message);
            }

            return sb.ToString();
        }
    }
}
